package com.changyinBus.system.service.impl;

import com.changyinBus.common.core.domain.TreeSelect;
import com.changyinBus.common.core.domain.entity.SysCate;
import com.changyinBus.common.utils.StringUtils;
import com.changyinBus.system.mapper.SysCateMapper;
import com.changyinBus.system.service.ISysCateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysCateServiceImpl implements ISysCateService {
    @Resource
    private SysCateMapper sysCateMapper;

    @Override
    public List<SysCate> selectCateList(SysCate cate) {
        return sysCateMapper.selectCateList(cate);
    }

    @Override
    public SysCate selectCateById(Long cateId) {
        return sysCateMapper.selectCateById(cateId);
    }

    @Override
    public SysCate selectSingleCate(SysCate cate) {
        return sysCateMapper.selectSingleCate(cate);
    }

    @Override
    public List<SysCate> selectChildrenCateById(Long cateId) {
        return sysCateMapper.selectChildrenCateById(cateId);
    }

    @Override
    public int hasChildByCateId(Long cateId) {
        return sysCateMapper.hasChildByCateId(cateId);
    }

    @Override
    public int insertCate(SysCate cate) {
        return sysCateMapper.insertCate(cate);
    }

    @Override
    public int updateCate(SysCate cate) {
        return sysCateMapper.updateCate(cate);
    }

    @Override
    public int updateCateChildren(List<SysCate> cates) {
        return sysCateMapper.updateCateChildren(cates);
    }

    @Override
    public int deleteCateById(Long cateId) {
        return sysCateMapper.deleteCateById(cateId);
    }

    @Override
    public List<TreeSelect> selectCateTreeList(SysCate cate) {
        cate.setParentId(-1);
        List<SysCate> cates = selectCateList(cate);
        return buildCateTreeSelect(cates);
    }

    @Override
    public List<TreeSelect> buildCateTreeSelect(List<SysCate> cates) {
        List<SysCate> deptTrees = buildDeptTree(cates);
        return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<SysCate> buildDeptTree(List<SysCate> cates) {
        List<SysCate> returnList = new ArrayList<SysCate>();
        List<Long> tempList = cates.stream().map(SysCate::getCateId).collect(Collectors.toList());
        for (SysCate cate : cates) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(cate.getParentId())) {
                recursionFn(cates, cate);
                returnList.add(cate);
            }
        }
        if (returnList.isEmpty()) {
            returnList = cates;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysCate> list, SysCate t) {
        // 得到子节点列表
        List<SysCate> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysCate tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysCate> getChildList(List<SysCate> list, SysCate t) {
        List<SysCate> tlist = new ArrayList<SysCate>();
        Iterator<SysCate> it = list.iterator();
        while (it.hasNext()) {
            SysCate n = (SysCate) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId() == t.getCateId()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysCate> list, SysCate t) {
        return getChildList(list, t).size() > 0;
    }
}
